package com.example.demo02.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo02.mapper.ProductMapper;
import com.example.demo02.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

/**
 * (Products)表服务实现类
 *
 * @author makejava
 * @since 2024-09-01 20:15:30
 */
@Service("productService")
public class ProductService extends ServiceImpl<ProductMapper, Product>{
    @Autowired
    private ProductMapper productMapper;

    // 增加库存
    public void increaseStock(Integer productId, int quantity) {
        Product product = productMapper.selectById(productId);
        if (product == null) {
            throw new RuntimeException("没有找到该商品");
        }
        product.setStockQuantity(product.getStockQuantity() + quantity);
        productMapper.updateByIdQu(productId, quantity);
    }

    // 减少库存
    public boolean reduceStock(Integer productId, int quantity) {
        Product product = productMapper.selectById(productId);
        if (product == null) {
            throw new RuntimeException("没有找到该商品");
        }
        if (product.getStockQuantity() >= quantity) {
            product.setStockQuantity(product.getStockQuantity() - quantity);
            int updatedRecords = productMapper.updateByIdQu(productId, -quantity);
            return updatedRecords > 0;
        }
        return false;
    }

}


